package com.xju.atpgs.service.Impl;

import com.xju.atpgs.entity.*;
import com.xju.atpgs.entity.Dictionary;
import com.xju.atpgs.generator.GeneticAlgorithm;
import com.xju.atpgs.generator.GeneticParameter;
import com.xju.atpgs.generator.Paper;
import com.xju.atpgs.generator.Population;
import com.xju.atpgs.mapper.ExamMapper;
import com.xju.atpgs.service.DictionaryService;
import com.xju.atpgs.service.ExamService;
import com.xju.atpgs.service.QuestionService;
import com.xju.atpgs.service.UserService;
import com.xju.atpgs.utils.DateUtil;
import com.xju.atpgs.utils.IDGenerator;
import com.xju.atpgs.utils.SecurityUserHelper;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private ExamService examService;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private UserService userService;

    @Override
    public String autoGenerator(ExamRequest examRequest)  {
        String examName=examRequest.getExamName();
        int choice=Integer.parseInt(examRequest.getChoice());
        int judge=Integer.parseInt(examRequest.getJudge());
        int filling=Integer.parseInt(examRequest.getFilling());
        int shortAnswer=Integer.parseInt(examRequest.getShortAnswer());
        int solve=Integer.parseInt(examRequest.getSolve());
        Paper paper=geneticAlgorithmGenerator(examRequest);
        DecimalFormat decimalFormat=new DecimalFormat("#.00");
        paper.setDifficulty(Double.parseDouble(decimalFormat.format(paper.getDifficulty())));
        List<Question> choiceList=paper.getChoiceQuestion();
        List<Question> judgeList=paper.getJudgeQuestion();
        List<Question> fillingList=paper.getFillingQuestion();
        List<Question> shortAnswerList=paper.getShortAnswerQuestion();
        List<Question> solveList=paper.getSolveQuestion();
        XWPFDocument document=new XWPFDocument();
        String filePath="D:/download/";
        String fileName=examName+".docx";
        try {
            File file=new File(filePath+fileName);
            File parentFile=file.getParentFile();
            if (!parentFile.exists()){
                parentFile.mkdirs();
            }
            if (!file.exists()){
                file.createNewFile();
            }
            FileOutputStream fileOutputStream=new FileOutputStream(file);
            XWPFParagraph p1=document.createParagraph();
            p1.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun p1Run=p1.createRun();
            p1Run.setText(examName);
            p1Run.setFontFamily("宋体");
            p1Run.setFontSize(18);
            p1Run.setBold(true);
            XWPFTable t1=document.createTable(2,7);


            CTTbl ttbl = t1.getCTTbl();
            CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
            CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
            CTJc cTJc=tblPr.addNewJc();
            cTJc.setVal(STJc.Enum.forString("center"));
            tblWidth.setW(new BigInteger("8000"));
            tblWidth.setType(STTblWidth.DXA);

            List<XWPFTableRow> rows = t1.getRows();
            for(XWPFTableRow row : rows) {
                CTTrPr trPr = row.getCtRow().addNewTrPr();
                CTHeight ht = trPr.addNewTrHeight();
                ht.setVal(BigInteger.valueOf(800));
                List<XWPFTableCell> cells = row.getTableCells();
                for(XWPFTableCell tableCell : cells ) {
                    CTTcPr cttcpr = tableCell.getCTTc().addNewTcPr();
                    cttcpr.addNewVAlign().setVal(STVerticalJc.CENTER);
                }
            }

            XWPFParagraph k1=t1.getRow(0).getCell(0).getParagraphArray(0);
            k1.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k1Run=k1.createRun();
            k1Run.setText("题号");
            k1Run.setFontFamily("宋体");
            k1Run.setFontSize(14);
            XWPFParagraph k2=t1.getRow(0).getCell(1).getParagraphArray(0);
            k2.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k2Run=k2.createRun();
            k2Run.setText("一");
            k2Run.setFontFamily("宋体");
            k2Run.setFontSize(14);
            XWPFParagraph k3=t1.getRow(0).getCell(2).getParagraphArray(0);
            k3.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k3Run=k3.createRun();
            k3Run.setText("二");
            k3Run.setFontFamily("宋体");
            k3Run.setFontSize(14);
            XWPFParagraph k4=t1.getRow(0).getCell(3).getParagraphArray(0);
            k4.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k4Run=k4.createRun();
            k4Run.setText("三");
            k4Run.setFontFamily("宋体");
            k4Run.setFontSize(14);
            XWPFParagraph k5=t1.getRow(0).getCell(4).getParagraphArray(0);
            k5.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k5Run=k5.createRun();
            k5Run.setText("四");
            k5Run.setFontFamily("宋体");
            k5Run.setFontSize(14);
            XWPFParagraph k6=t1.getRow(0).getCell(5).getParagraphArray(0);
            k6.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k6Run=k6.createRun();
            k6Run.setText("五");
            k6Run.setFontFamily("宋体");
            k6Run.setFontSize(14);
            XWPFParagraph k7=t1.getRow(0).getCell(6).getParagraphArray(0);
            k7.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k7Run=k7.createRun();
            k7Run.setText("总分");
            k7Run.setFontFamily("宋体");
            k7Run.setFontSize(14);

            XWPFParagraph p2=document.createParagraph();
            XWPFRun p2Run=p2.createRun();
            p2Run.setText("一、单项选择题（将一个正确的答案代码填入到括号中每小题"+examRequest.getChoiceScore()+"分，共"+Integer.valueOf(examRequest.getChoiceScore())*Integer.valueOf(examRequest.getChoice())+"分）");
            p2Run.setFontFamily("宋体");
            p2Run.setFontSize(14);
            p2Run.setBold(true);
            for (Question question:choiceList){
                String index=String.valueOf(choiceList.indexOf(question)+1);
                XWPFParagraph paragraph=document.createParagraph();
                paragraph.setSpacingBetween(20, LineSpacingRule.EXACT);
                XWPFRun run=paragraph.createRun();
                String[] str=question.getQuesContent().split("A");
                run.setFontFamily("宋体");
                run.setText(index+"、"+str[0]);
                run.setFontSize(12);
                XWPFParagraph paragraph1=document.createParagraph();
                paragraph1.setSpacingBetween(20,LineSpacingRule.EXACT);
                XWPFRun run1=paragraph1.createRun();
                run1.setText("A"+str[1]);
                run1.setFontFamily("宋体");
                run1.setFontSize(12);
            }
            XWPFParagraph z1=document.createParagraph();
            XWPFRun zrun=z1.createRun();
            zrun.setText("二、判断题（正确的在括号内打上“√”，错误的打上“×”。每小题"+examRequest.getJudgeScore()+"分，共"+Integer.valueOf(examRequest.getJudgeScore())*Integer.valueOf(examRequest.getJudge())+"分）");
            zrun.setFontFamily("宋体");
            zrun.setFontSize(14);
            zrun.setBold(true);
            for (Question question:judgeList){
                String index=String.valueOf(judgeList.indexOf(question)+1);
                XWPFParagraph paragraph=document.createParagraph();
                paragraph.setSpacingBetween(20,LineSpacingRule.EXACT);
                paragraph.setSpacingBetween(20, LineSpacingRule.EXACT);
                XWPFRun run=paragraph.createRun();
                run.setText(index+"、"+question.getQuesContent());
                run.setFontFamily("宋体");
                run.setFontSize(12);
            }
            XWPFParagraph c=document.createParagraph();
            XWPFRun cRun=c.createRun();
            cRun.setText("三、填空题（在横线上填写正确答案。每小题"+examRequest.getFillingScore()+"分，共"+Integer.valueOf(examRequest.getFillingScore())*Integer.valueOf(examRequest.getFilling())+"分）");
            cRun.setFontFamily("宋体");
            cRun.setFontSize(14);
            cRun.setBold(true);
            for (Question question:fillingList){
                String index=String.valueOf(fillingList.indexOf(question)+1);
                XWPFParagraph paragraph=document.createParagraph();
                paragraph.setSpacingBetween(20,LineSpacingRule.EXACT);
                XWPFRun run=paragraph.createRun();
                run.setText(index+"、"+question.getQuesContent());
                run.setFontFamily("宋体");
                run.setFontSize(12);
            }
            XWPFParagraph d=document.createParagraph();
            XWPFRun dRun=d.createRun();
            dRun.setText("四、问答题（每小题"+examRequest.getShortScore()+"分，共"+Integer.valueOf(examRequest.getShortScore())*Integer.valueOf(examRequest.getFilling())+"分）");
            dRun.setFontFamily("宋体");
            dRun.setFontSize(14);
            dRun.setBold(true);
            for (Question question:shortAnswerList){
                String index=String.valueOf(shortAnswerList.indexOf(question)+1);
                XWPFParagraph paragraph=document.createParagraph();
                paragraph.setSpacingBetween(20,LineSpacingRule.EXACT);
                XWPFRun run=paragraph.createRun();
                run.setText(index+"、"+question.getQuesContent());
                run.setFontSize(12);
                run.setFontFamily("宋体");
                XWPFParagraph paragraph1=document.createParagraph();
                paragraph1.setSpacingBetween(20,LineSpacingRule.EXACT);
                XWPFRun run1=paragraph.createRun();
            }
            XWPFParagraph e=document.createParagraph();
            XWPFRun eRun=e.createRun();
            eRun.setText("五、应用题（每小题"+examRequest.getSolveScore()+"分，共"+Integer.valueOf(examRequest.getSolveScore())*Integer.valueOf(examRequest.getSolve())+"分）");
            eRun.setFontFamily("宋体");
            eRun.setFontSize(14);
            eRun.setBold(true);
            for (Question question:solveList){
                String index=String.valueOf(solveList.indexOf(question)+1);
                XWPFParagraph paragraph=document.createParagraph();
                paragraph.setSpacingBetween(20,LineSpacingRule.EXACT);
                XWPFRun run=paragraph.createRun();
                run.setText(index+"、"+question.getQuesContent());
                run.setFontSize(12);
                run.setFontFamily("宋体");
                XWPFParagraph paragraph1=document.createParagraph();
                paragraph1.setSpacingBetween(20,LineSpacingRule.EXACT);
                XWPFRun run1=paragraph.createRun();
            }
            document.write(fileOutputStream);
            fileOutputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Exam exam=new Exam();
        exam.setExamId(IDGenerator.getId());
        exam.setExamName(examName);
        exam.setExamContent(filePath+fileName);
        Date date=new Date();
        exam.setExamCreateTime(date);
        Dictionary dictionary=dictionaryService.findByTypeAndValue("试卷状态","待审核");
        exam.setExamStatus(dictionary.getDicId());
        UserDetails userDetails=(UserDetails) SecurityUserHelper.getCurrentUser();
        User user=userService.findByEmail(userDetails.getUsername());
        exam.setExamCreator(user.getUserId());
        exam.setExamDifficulty(String.valueOf(paper.getDifficulty()));
        exam.setExamScore(String.valueOf(paper.getGeneticParameter().getTotalScore()));
        examService.insertExam(exam);
        return filePath+fileName ;
    }

    @Override
    public List<Question> getExamQuestion(String type,int number){
        List<Question> questionList=questionService.findByType(type);
        List<Question> paper=new ArrayList<>();
        Random random=new Random();
        int size=questionList.size();
        int index[]= new int[size];
        Arrays.fill(index,0);
        for(int i=0;i<number;i++){
            boolean flag=true;
            int j=0;
            while(flag){
                j=random.nextInt(size);
                if(index[j]==0){
                    flag=false;
                    index[j]=1;
                }
            }
            Question question=questionList.get(j);
            paper.add(question);
        }
        return paper;
    }

    @Override
    public String manualGenerator(String[] data,ExamRequest examRequest) {
        List<Question> choiceList=new ArrayList<>();
        List<Question> judgeList=new ArrayList<>();
        List<Question> fillingList=new ArrayList<>();
        List<Question> shortAnswerList=new ArrayList<>();
        List<Question> solveList=new ArrayList<>();
        for (String id:data){
            Question question=questionService.findbyId(id);
            Dictionary dictionary=dictionaryService.findById(question.getQuesType());
            switch (dictionary.getDicValue()){
                case "选择题":choiceList.add(question);break;
                case "判断题":judgeList.add(question);break;
                case "填空题":fillingList.add(question);break;
                case "简答题":shortAnswerList.add(question);break;
                case "应用题":solveList.add(question);break;
            }
        }
        String examName=examRequest.getExamName();
        double choiceScore=Integer.parseInt(examRequest.getChoiceScore());
        double judgeScore=Integer.parseInt(examRequest.getJudgeScore());
        double fillingScore=Integer.parseInt(examRequest.getFillingScore());
        double shortAnswerScore=Integer.parseInt(examRequest.getShortScore());
        double solveScore=Integer.parseInt(examRequest.getSolveScore());
        XWPFDocument document=new XWPFDocument();
        String filePath="D:/download/";
        String fileName=examName+".docx";
        try {
            File file=new File(filePath+fileName);
            File parentFile=file.getParentFile();
            if (!parentFile.exists()){
                parentFile.mkdirs();
            }
            if (!file.exists()){
                file.createNewFile();
            }
            FileOutputStream fileOutputStream=new FileOutputStream(file);
            XWPFParagraph p1=document.createParagraph();
            p1.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun p1Run=p1.createRun();
            p1Run.setText(examName);
            p1Run.setFontFamily("宋体");
            p1Run.setFontSize(18);
            p1Run.setBold(true);
            XWPFTable t1=document.createTable(2,7);


            CTTbl ttbl = t1.getCTTbl();
            CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
            CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
            CTJc cTJc=tblPr.addNewJc();
            cTJc.setVal(STJc.Enum.forString("center"));
            tblWidth.setW(new BigInteger("8000"));
            tblWidth.setType(STTblWidth.DXA);

            List<XWPFTableRow> rows = t1.getRows();
            for(XWPFTableRow row : rows) {
                CTTrPr trPr = row.getCtRow().addNewTrPr();
                CTHeight ht = trPr.addNewTrHeight();
                ht.setVal(BigInteger.valueOf(800));
                List<XWPFTableCell> cells = row.getTableCells();
                for(XWPFTableCell tableCell : cells ) {
                    CTTcPr cttcpr = tableCell.getCTTc().addNewTcPr();
                    cttcpr.addNewVAlign().setVal(STVerticalJc.CENTER);
                }
            }

            XWPFParagraph k1=t1.getRow(0).getCell(0).getParagraphArray(0);
            k1.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k1Run=k1.createRun();
            k1Run.setText("题号");
            k1Run.setFontFamily("宋体");
            k1Run.setFontSize(14);
            XWPFParagraph k2=t1.getRow(0).getCell(1).getParagraphArray(0);
            k2.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k2Run=k2.createRun();
            k2Run.setText("一");
            k2Run.setFontFamily("宋体");
            k2Run.setFontSize(14);
            XWPFParagraph k3=t1.getRow(0).getCell(2).getParagraphArray(0);
            k3.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k3Run=k3.createRun();
            k3Run.setText("二");
            k3Run.setFontFamily("宋体");
            k3Run.setFontSize(14);
            XWPFParagraph k4=t1.getRow(0).getCell(3).getParagraphArray(0);
            k4.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k4Run=k4.createRun();
            k4Run.setText("三");
            k4Run.setFontFamily("宋体");
            k4Run.setFontSize(14);
            XWPFParagraph k5=t1.getRow(0).getCell(4).getParagraphArray(0);
            k5.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k5Run=k5.createRun();
            k5Run.setText("四");
            k5Run.setFontFamily("宋体");
            k5Run.setFontSize(14);
            XWPFParagraph k6=t1.getRow(0).getCell(5).getParagraphArray(0);
            k6.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k6Run=k6.createRun();
            k6Run.setText("五");
            k6Run.setFontFamily("宋体");
            k6Run.setFontSize(14);
            XWPFParagraph k7=t1.getRow(0).getCell(6).getParagraphArray(0);
            k7.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun k7Run=k7.createRun();
            k7Run.setText("总分");
            k7Run.setFontFamily("宋体");
            k7Run.setFontSize(14);

            XWPFParagraph p2=document.createParagraph();
            XWPFRun p2Run=p2.createRun();
            p2Run.setText("一、单项选择题（将一个正确的答案代码填入到括号中每小题"+examRequest.getChoiceScore()+"分，共"+Integer.valueOf(examRequest.getChoiceScore())*Integer.valueOf(examRequest.getChoice())+"分）");
            p2Run.setFontFamily("宋体");
            p2Run.setFontSize(14);
            p2Run.setBold(true);
            for (Question question:choiceList){
                String index=String.valueOf(choiceList.indexOf(question)+1);
                XWPFParagraph paragraph=document.createParagraph();
                paragraph.setSpacingBetween(20, LineSpacingRule.EXACT);
                XWPFRun run=paragraph.createRun();
                String[] str=question.getQuesContent().split("A");
                run.setFontFamily("宋体");
                run.setText(index+"、"+str[0]);
                run.setFontSize(12);
                XWPFParagraph paragraph1=document.createParagraph();
                paragraph1.setSpacingBetween(20,LineSpacingRule.EXACT);
                XWPFRun run1=paragraph1.createRun();
                run1.setText("A"+str[1]);
                run1.setFontFamily("宋体");
                run1.setFontSize(12);
            }
            XWPFParagraph z1=document.createParagraph();
            XWPFRun zrun=z1.createRun();
            zrun.setText("二、判断题（正确的在括号内打上“√”，错误的打上“×”。每小题"+examRequest.getJudgeScore()+"分，共"+Integer.valueOf(examRequest.getJudgeScore())*Integer.valueOf(examRequest.getJudge())+"分）");
            zrun.setFontFamily("宋体");
            zrun.setFontSize(14);
            zrun.setBold(true);
            for (Question question:judgeList){
                String index=String.valueOf(judgeList.indexOf(question)+1);
                XWPFParagraph paragraph=document.createParagraph();
                paragraph.setSpacingBetween(20,LineSpacingRule.EXACT);
                paragraph.setSpacingBetween(20, LineSpacingRule.EXACT);
                XWPFRun run=paragraph.createRun();
                run.setText(index+"、"+question.getQuesContent());
                run.setFontFamily("宋体");
                run.setFontSize(12);
            }
            XWPFParagraph c=document.createParagraph();
            XWPFRun cRun=c.createRun();
            cRun.setText("三、填空题（在横线上填写正确答案。每小题"+examRequest.getFillingScore()+"分，共"+Integer.valueOf(examRequest.getFillingScore())*Integer.valueOf(examRequest.getFilling())+"分）");
            cRun.setFontFamily("宋体");
            cRun.setFontSize(14);
            cRun.setBold(true);
            for (Question question:fillingList){
                String index=String.valueOf(fillingList.indexOf(question)+1);
                XWPFParagraph paragraph=document.createParagraph();
                paragraph.setSpacingBetween(20,LineSpacingRule.EXACT);
                XWPFRun run=paragraph.createRun();
                run.setText(index+"、"+question.getQuesContent());
                run.setFontFamily("宋体");
                run.setFontSize(12);
            }
            XWPFParagraph d=document.createParagraph();
            XWPFRun dRun=d.createRun();
            dRun.setText("四、问答题（每小题"+examRequest.getShortScore()+"分，共"+Integer.valueOf(examRequest.getShortScore())*Integer.valueOf(examRequest.getFilling())+"分）");
            dRun.setFontFamily("宋体");
            dRun.setFontSize(14);
            dRun.setBold(true);
            for (Question question:shortAnswerList){
                String index=String.valueOf(shortAnswerList.indexOf(question)+1);
                XWPFParagraph paragraph=document.createParagraph();
                paragraph.setSpacingBetween(20,LineSpacingRule.EXACT);
                XWPFRun run=paragraph.createRun();
                run.setText(index+"、"+question.getQuesContent());
                run.setFontSize(12);
                run.setFontFamily("宋体");
                XWPFParagraph paragraph1=document.createParagraph();
                paragraph1.setSpacingBetween(20,LineSpacingRule.EXACT);
                XWPFRun run1=paragraph.createRun();
            }
            XWPFParagraph e=document.createParagraph();
            XWPFRun eRun=e.createRun();
            eRun.setText("五、应用题（每小题"+examRequest.getSolveScore()+"分，共"+Integer.valueOf(examRequest.getSolveScore())*Integer.valueOf(examRequest.getSolve())+"分）");
            eRun.setFontFamily("宋体");
            eRun.setFontSize(14);
            eRun.setBold(true);
            for (Question question:solveList){
                String index=String.valueOf(solveList.indexOf(question)+1);
                XWPFParagraph paragraph=document.createParagraph();
                paragraph.setSpacingBetween(20,LineSpacingRule.EXACT);
                XWPFRun run=paragraph.createRun();
                run.setText(index+"、"+question.getQuesContent());
                run.setFontSize(12);
                run.setFontFamily("宋体");
                XWPFParagraph paragraph1=document.createParagraph();
                paragraph1.setSpacingBetween(20,LineSpacingRule.EXACT);
                XWPFRun run1=paragraph.createRun();
            }
            document.write(fileOutputStream);
            fileOutputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Exam exam=new Exam();
        exam.setExamId(IDGenerator.getId());
        exam.setExamName(examName);
        exam.setExamContent(filePath+fileName);
        Date date=new Date();
        exam.setExamCreateTime(date);
        Dictionary dictionary=dictionaryService.findByTypeAndValue("试卷状态","待审核");
        exam.setExamStatus(dictionary.getDicId());
        UserDetails userDetails=(UserDetails) SecurityUserHelper.getCurrentUser();
        User user=userService.findByEmail(userDetails.getUsername());
        exam.setExamCreator(user.getUserId());
        ExamQuestion examQuestion=new ExamQuestion(choiceList,judgeList,fillingList,shortAnswerList,solveList,Integer.parseInt(examRequest.getChoiceScore()),Integer.parseInt(examRequest.getJudgeScore()),Integer.parseInt(examRequest.getFillingScore()),Integer.parseInt(examRequest.getShortScore()),Integer.parseInt(examRequest.getSolveScore()));
        exam.setExamDifficulty(examQuestion.getDifficulty());
        exam.setExamScore(examQuestion.getTotalScore());
        examService.insertExam(exam);
        return filePath+fileName ;
    }


    @Override
    public List<ResultExam> examManager() {
        Dictionary dictionary=dictionaryService.findByTypeAndValue("试卷状态","可用");
        List<Exam> examList=examService.findByStatus(dictionary.getDicId());
        List<ResultExam> resultExamList =new ArrayList<>();
        for (Exam exam:examList){
            User user=userService.findById(exam.getExamCreator());
            Dictionary dictionary1=dictionaryService.findById(exam.getExamStatus());
            String date= DateUtil.dateFormat(exam.getExamCreateTime());
            ResultExam resultExam=new ResultExam(user.getUserName(),date,dictionary1.getDicValue(),exam);
            resultExamList.add(resultExam);
        }
        return resultExamList;
    }

    @Override
    public List<Exam> findByStatus(String type) {
        ExamExample examExample=new ExamExample();
        examExample.createCriteria().andExamStatusEqualTo(type);
        List<Exam> examList=examMapper.selectByExampleWithBLOBs(examExample);
        return examList;
    }

    @Override
    public void insertExam(Exam exam) {
        examMapper.insert(exam);
    }

    @Override
    public List<Exam> findAll() {
        ExamExample examExample=new ExamExample();
        examExample.createCriteria().andExamIdIsNotNull();
        List<Exam> examList=examMapper.selectByExampleWithBLOBs(examExample);
        return examList;
    }

    @Override
    public Paper geneticAlgorithmGenerator(ExamRequest examRequest) {
        List<Question> choiceQuestion = questionService.findByType("选择题");
        List<Question> judgeQuestion = questionService.findByType("判断题");
        List<Question> fillingQuestion = questionService.findByType("填空题");
        List<Question> shortAnswerQuestion = questionService.findByType("简答题");
        List<Question> solveQuestion = questionService.findByType("应用题");
        GeneticParameter geneticParameter=new GeneticParameter(choiceQuestion,judgeQuestion,fillingQuestion,shortAnswerQuestion,solveQuestion,20,0.7,0.015,Double.parseDouble(examRequest.getExpect()),Integer.parseInt(examRequest.getChoice()) ,Integer.parseInt(examRequest.getJudge()),  Integer.parseInt(examRequest.getFilling()),Integer.parseInt(examRequest.getShortAnswer()),Integer.parseInt(examRequest.getSolve()),Double.parseDouble(examRequest.getChoiceScore()),Double.parseDouble(examRequest.getJudgeScore()),Double.parseDouble(examRequest.getFillingScore()),Double.parseDouble(examRequest.getShortScore()),Double.parseDouble(examRequest.getSolveScore()));
        int count=0;
        Population population=new Population(geneticParameter);
        Paper best=new Paper(geneticParameter);
        while (count<50){
            count++;
            population= GeneticAlgorithm.evolvePopulation(population);
            best=population.getBest();
        }
        return best;
    }


}
